<%@page import="java.util.Stack"%>
<div id="header"><jsp:include page="header.jsp"/></div>
<br/>
<%@page import="java.util.Iterator"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="Servlet.dataBaseController"%>
<%@page import="java.util.Vector"%>
<%@page import="java.sql.*"%>
<%@page import="com.mysql.jdbc.Connection"%>
<div id="page-wrap">
        <div id="main-sidebar">
            <!-- Below the including content will based on user's role in the system  -->
            <%
             JavaBeans.User user = (JavaBeans.User) session.getAttribute("user");
             if(user != null)
             {
            
             if(user.getRole() == 'e'){%>
                <jsp:include page="Left Panel/CustomerRep.jsp"/>
                <%}%>
             <%if(user.getRole() == 'm'){%>
                <jsp:include page="Left Panel/Manager.jsp"/>
                <%}%>
             <%if(user.getRole() == 'c'){%>
                <jsp:include page="Left Panel/Customer.jsp"/>
            <%}
                         }%>
            
        </div>
<div style="overflow: auto" id="main-content">
        <!--[if IE]><script language="javascript" type="text/javascript" src="../excanvas.pack.js"></script><![endif]-->
        <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js'></script>
        <script src="js/amstock.js" type="text/javascript"></script>

        <%
            Connection connection = null;
            Statement st = null;
            ResultSet rs = null;
            String strQuery = null;
            Stack pi = new Stack();
            Stack di = new Stack();
            Stack year = new Stack();
            Stack month = new Stack();
            Stack day = new Stack();
            int startDate;
            int endDate;
            
            try {
                connection = dataBaseController.getConnection();
            } catch (Exception e) {
                System.out.println("Error:");
            }

            int c = 0;
            try {
                st = connection.createStatement();
                strQuery = "Select S.PricePerShare, S.Date FROM `brchung`.`stockhistory` S WHERE S.stockid = '" + request.getParameter("symbol") + "' ORDER by S.Date";
                rs = st.executeQuery(strQuery);
                Stack temp = new Stack();
                Stack tempMon = new Stack();
                Stack tempDay = new Stack();
                Stack tempYear = new Stack();
                while (rs.next()) {
                    String price = rs.getString("PricePerShare");
                    Date d = rs.getDate("Date");
                    float p = Float.valueOf(price.trim()).floatValue();
                    String[] date=d.toString().split("-");
                    //Date newD=new Date(Integer.parseInt(date[0]),d.getMonth(),Integer.parseInt(date[1]));
                    //out.println(d.getMonth()+"<br/>");
                    if(!di.contains(d.getTime())){
                    tempYear.push(Integer.parseInt(date[0]));
                    tempMon.push(d.getMonth());
                    tempDay.push(Integer.parseInt(date[2]));
                    temp.push(p);
                    di.push(d.getTime());
                    }
                    c++;
                }
                //out.println(t);
                while(!temp.isEmpty()){//credited by Jin
                    pi.push(temp.pop());
                    year.push(tempYear.pop());
                    month.push(tempMon.pop());
                    day.push(tempDay.pop());
                }
                    
                
            } catch (Exception e) {
            }
        %>

        <h3><%=request.getParameter("symbol")%> History</h3>
        <div id="chartdiv" style="width:100%; height:90%;"></div>
        <script type="text/javascript">
			AmCharts.ready(function () {
				generateChartData();
				createStockChart();
			});

			var chartData = [];

			function generateChartData() {
				var firstDate = new Date(2012, 0, 1);
				firstDate.setDate(firstDate.getDate() - 500)
				firstDate.setHours(0, 0, 0, 0);
                                
                                <% for(int i=0;i<di.size();i++){ %>
                                   var d=new Date(<%= year.pop() %>,<%= month.pop() %>,<%= day.pop() %>);
                                   d.setHours(0, 0, 0, 0);
                                   chartData.push({
						date: d,
						value: <%= pi.pop() %>
					});
                                   <% } %>
                            
			}


			function createStockChart() {
				var chart = new AmCharts.AmStockChart();
				chart.pathToImages = "img/";

				// DATASETS //////////////////////////////////////////
				var dataSet = new AmCharts.DataSet();
				dataSet.color = "#b0de09";
				dataSet.fieldMappings = [{
					fromField: "value",
					toField: "value"
				}];
				dataSet.dataProvider = chartData;
				dataSet.categoryField = "date";

				chart.dataSets = [dataSet];

				// PANELS ///////////////////////////////////////////                                                  
				var stockPanel = new AmCharts.StockPanel();
				stockPanel.showCategoryAxis = true;
				stockPanel.title = "Value";
				stockPanel.eraseAll = false;
                                <% if(di.size()>0){ %>
                                    stockPanel.addLabel(0, 100, "Click on the pencil icon on top-right to start drawing", "center", 16);
                                <% }else{ %>
                                    stockPanel.addLabel(0, 100, "There's currently no data for this stock symbol", "center", 16);
                                    <% } %>
				var graph = new AmCharts.StockGraph();
				graph.valueField = "value";
				graph.bullet = "round";
				stockPanel.addStockGraph(graph);

				var stockLegend = new AmCharts.StockLegend();
				stockLegend.valueTextRegular = " ";
				stockLegend.markerType = "none";
				stockPanel.stockLegend = stockLegend;
				stockPanel.drawingIconsEnabled = true;

				chart.panels = [stockPanel];


				// OTHER SETTINGS ////////////////////////////////////
				var scrollbarSettings = new AmCharts.ChartScrollbarSettings();
				scrollbarSettings.graph = graph;
				scrollbarSettings.updateOnReleaseOnly = true;
				chart.chartScrollbarSettings = scrollbarSettings;

				var cursorSettings = new AmCharts.ChartCursorSettings();
				cursorSettings.valueBalloonsEnabled = true;
				chart.chartCursorSettings = cursorSettings;


				// PERIOD SELECTOR ///////////////////////////////////
				var periodSelector = new AmCharts.PeriodSelector();
				periodSelector.position = "bottom";
				periodSelector.periods = [{
					period: "DD",
					count: 10,
					label: "10 days"
				}, {
					period: "MM",
					count: 1,
					label: "1 month"
				}, {
					period: "YYYY",
					count: 1,
					label: "1 year"
				}, {
					period: "YTD",
					label: "YTD"
				}, {
					period: "MAX",
					label: "MAX"
				}];
				chart.periodSelector = periodSelector;

				var panelsSettings = new AmCharts.PanelsSettings();
				chart.panelsSettings = panelsSettings;

				chart.write('chartdiv');
			}


			
		</script>
        </div></div>
    <div id="footer" align="center">
        <jsp:include page="footer.jsp"/>
    </div>
</body>
</html>